零成本学arduino教程

您所在的位置:网站首页 Arduino 模拟量读取不稳定 零成本学arduino教程

零成本学arduino教程

2023-12-01 08:10| 来源: 网络整理| 查看: 265

光敏传感器

在这里插入图片描述 光阻器是一个模拟传感器,作用类似于可变电阻器。当传感器暴露在光线下时,传感器的电阻降低。我们可以使用模拟输入引脚读取光电阻器上的电压,这与光阻器的电阻直接相关。

引脚名称 名字描述VCC正电源GND地DO数字输出AO模拟输出 操作

光阻传感器模块包括一个LDR(光依赖电阻器)系列与10K电阻器。AO 销连接 LDR 和 10K 电阻器。 在这里插入图片描述

AO 端上的电压取决于照明 - 即落在传感器上的光量。您可以通过将光驱光传感器的 AO 端连接到模拟输入引脚,然后使用该功能来读取此电压。analogRead()

条件光照强度LDR 阻值电压analogRead(value)满月0.11.25MΩ4.961016深暮1250kΩ4.81985黄昏1050kΩ4.17853计算机监视器5016.2kΩ3.09633楼梯照明1009.98kΩ2.50511办公室照明4003.78kΩ1.37281阴天1,0001.99kΩ0.83170全日光10,000397Ω0.1939直射100,00079Ω0.048 代码解析: 换算函数analogRead() // 根据表格选择正确的GAMMA和RL10 const float GAMMA = 0.7; const float RL10 = 50; //光照强度为10,相当于表格黄昏的电阻50K欧 // Convert the analog value into lux value: int analogValue = analogRead(A0); float voltage = analogValue / 1024. * 5; float resistance = 2000 * voltage / (1 - voltage / 5); float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

将返回值转换为照明值的以下代码(在 lux 中):analogRead() ①RL10:光敏电阻在10lux照明水平下的电阻,查表可得:光照强度为10,相当于表格黄昏的电阻50K欧。 ②GAMMA:log(电阻值®) / log(光照强度(lux))图的斜率

LCD位置设置函数lcd.setCursor(2, 0) 参数 2:开头跳过两格 0:第一行 在这里插入图片描述 模拟器示例

1.数字仿真 在这里插入图片描述 点击此处Photoresistor Digital Example开始仿真

2.模拟仿真 在这里插入图片描述 点击此处Photoresistor Analog Example开始仿真

源代码

1.数字仿真代码

#include #define LDR_PIN 2 LiquidCrystal_I2C lcd(0x27, 20, 4); void setup() { pinMode(LDR_PIN, INPUT); lcd.init(); lcd.backlight(); } void loop() { lcd.setCursor(2, 0); lcd.print("Room: "); if (digitalRead(LDR_PIN) == LOW) { lcd.print("Light!"); } else { lcd.print("Dark "); } delay(100); }

2.模拟仿真代码

/* Photoresistor (LDR) Analog Demo Copyright (C) 2021 Uri Shaked. https://wokwi.com/arduino/projects/305193627138654786 */ #include #define LDR_PIN 2 // LDR Characteristics const float GAMMA = 0.7; const float RL10 = 50; LiquidCrystal_I2C lcd(0x27, 20, 4); void setup() { pinMode(LDR_PIN, INPUT); lcd.init(); lcd.backlight(); } void loop() { int analogValue = analogRead(A0); float voltage = analogValue / 1024. * 5; float resistance = 2000 * voltage / (1 - voltage / 5); float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)); lcd.setCursor(2, 0); lcd.print("Room: "); if (lux > 50) { lcd.print("Light!"); } else { lcd.print("Dark "); } lcd.setCursor(0, 1); lcd.print("Lux: "); lcd.print(lux); lcd.print(" "); delay(100); } 结束

在这里插入图片描述

不是每个人都能功成名就,我们中有些人注定要在日常生活的点滴中寻找生命的意义。 – 《生活大爆炸》



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3